<?php
namespace Battlelog;

class Mysql
{
	private $db;
	
	public function __construct()
    {
		//require_once( "./includes/connectdb.php" );
		$this->connectDb(DB_DSN, DB_USERNAME, DB_PASSWORD);
    }

    public function __destruct()
    {
        $this->db = null;
    }
		
    public function connectdb($db_dsn, $db_user, $db_pass)
    {
        try
		{
            $this->db = new \pdo($db_dsn, $db_user, $db_pass );
        }
		catch (\pdoexception $e)
		{
            echo "database error: " . $e->getmessage();
			file_put_contents('MysqlErrors.txt', $e->getMessage(), FILE_APPEND);  
            die();
        }
        $this->db->query('set names utf8');
        return $this;
    }
	
	public function UpdateUserTimeLogout($pid, $time)
	{
		$query = "update players set time=NOW()+".$time." where pid=".$pid."";
		$sth = $this->db->prepare($query);
		$sth->execute();
	}
	
	public function UpdateGravatar($file, $pid)
	{
		$query = "update players set gravatar='".$file."' where pid='".$pid."'";
		$sth = $this->db->prepare($query);
		$sth->execute();
	}
	
	public function ChangePass($dsnm, $password)
	{
		$query = "update players set password='".$password."' where dsnm='".$dsnm."'";
		$sth = $this->db->prepare($query);
		$sth->execute();
	}
	
	public function UpdateClantag($pid, $clntg, $dta, $dtb)
	{
		$dta = str_pad(dechex($dta), 3 ,'0', STR_PAD_LEFT);
		$dtb = str_pad(dechex($dtb), 3 ,'0', STR_PAD_LEFT);
		$temptag =str_pad($clntg, 4 ,'0', STR_PAD_LEFT);
		$tag = str_replace(' ','',ascii2hex(base64_decode(bf3Conv($temptag,1))));
		$uatt=hexdec($dta.$dtb.$tag);
		
		$query = "update players set clantag='".$clntg."', uatt='".$uatt."' where pid='".$pid."'";
		$sth = $this->db->prepare($query);
		$sth->execute();
	}
	
	public function LoadRank($pid, $time)
	{
		
	}
}
